<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { findListApi } from "@/api/system/dict"
import { type FormInstance } from "element-plus"
import { usePagination } from "@/hooks/usePagination"

defineOptions({
  name: "ComDataDict"
})

// 定义事件
const emit = defineEmits<{
  (event: "rowDbclick", row: any): void
}>()

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const pageData = ref({
  dialogErrorMessageVisible: false,
  dialogDetPage: false,
  currentShowErrorMessage: "",
  mainRowData: {} as any
})
const tableData = ref<any>([])
// Search
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  onlineCode: "",
  invoiceSn: ""
})

const getTableData = () => {
  loading.value = true
  findListApi({
    startPage: paginationData.startPage,
    pageSize: paginationData.pageSize,
    ...searchData
  })
    .then((res) => {
      paginationData.total = res.count
      tableData.value = res.data
      console.log(tableData.value)
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

const handleSearch = () => {
  paginationData.startPage === 1 ? getTableData() : (paginationData.startPage = 1)
}

const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}

// 双击事件
const rowDbclick = (row: any) => {
  console.log("选择了明细", row)
  emit("rowDbclick", row)
}

watch([() => paginationData.startPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" @row-dblclick="rowDbclick">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="dictName" label="字典名称" align="center" />
          <el-table-column prop="dataDictCode" label="字典代码" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="createUserName" label="创建人名称" align="center" />
          <el-table-column prop="modifiedTime" label="修改时间" align="center" />
          <el-table-column prop="modifiedUserName" label="修改人名称" align="center" />
        </el-table>
      </div>

      <!-- 分页控件 -->
      <!-- <PaginationControls
        :paginationData="paginationData"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
